package com.wcity.manage.web.property;

import com.alibaba.fastjson.JSON;
import com.wcity.manage.common.context.SessionKey;
import com.wcity.manage.common.utils.DwzUtil;
import com.wcity.manage.common.utils.RespUtil;
import com.wcity.manage.dao.dal.model.PageModel;
import com.wcity.manage.dao.model.ComplaintManage;
import com.wcity.manage.dao.model.User;
import com.wcity.manage.service.property.PropertyComplaintService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * Created by wang on 2017/7/19.
 */
@Controller
public class PropertyComplaintAct {
    @Resource
    private PropertyComplaintService propertyVoiceService;

    /**
     * 分页查询表单
     * @param voice
     * @param pageModel
     * @param request
     * @return
     */
    @RequestMapping("nvoice_list")
    public String voiceList (ComplaintManage voice , PageModel pageModel, HttpServletRequest request ) {
        User user = (User) request.getSession().getAttribute( SessionKey.SESSION_USER_INFO );
        voice.setUserId(user.getId());
        pageModel.setParams( voice );
        request.setAttribute( "pageModel", pageModel );
        request.setAttribute( "voiceInfo", voice );
        request.setAttribute( "userId", user.getId() );
        request.setAttribute( "workNo", user.getWorkNo() );
        request.setAttribute( "list", propertyVoiceService.getPayTypeListPage( pageModel ) );
        System.out.println(JSON.toJSONString(propertyVoiceService.getPayTypeListPage( pageModel )));
        return "propertyComplaintManage/voiceList";
    }

    /**
     * 跳转到回复页面
     * @return
     */
    @RequestMapping("nto_voice_Edit")
    public String toVoiceEdit ( HttpServletRequest request ,HttpServletResponse response ) {
        String id = request.getParameter("id");
        ComplaintManage voiceInfo =  propertyVoiceService.getComplaintManageInfo( id );
        if( voiceInfo.getStatus().equals("已处理") ){
            RespUtil.sendJson(response, DwzUtil.getRetAjaxJson("300", "您已经回复过了", "voice_list", "", "closeCurrent", ""));
            return null;
        }else{
            request.setAttribute( "list", voiceInfo );
            return "propertyComplaintManage/voiceEdit";
        }
    }

    /**
     * 回复
     * @param voice
     * @param response
     */
    @RequestMapping("nvoice_Edit")
    public void voiceEdit (ComplaintManage voice, HttpServletResponse response ) {
        String code = "200";
        String meg = "修改成功";
        try{
            voice.setStatus("已处理");
            voice.setReplyTime(new Date());
            ComplaintManage info = propertyVoiceService.getComplaintManageInfo( voice.getId()+"" );
            if(info.getStatus().equals("已处理")){
                RespUtil.sendJson( response, DwzUtil.getRetAjaxJson( "200", "已回复，无需再次提交", "nvoice_list", "", "closeCurrent", "" ) );
                return;
            }
            int success = propertyVoiceService.complaintManageEdit(voice);
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "nvoice_list", "", "closeCurrent", ""));
    }

    /**
     * 跳转到查看回复的页面
     * @param request
     * @return
     */
    @RequestMapping("nto_voice_reply")
    public String toVoiceReply( HttpServletRequest request , HttpServletResponse response ){
        String id = request.getParameter("id");
        request.setAttribute( "list", propertyVoiceService.getComplaintManageInfo( id,"已处理" ) );
        return "propertyComplaintManage/voiceReply";
    }

    /**
     * 跳转到修改回复的页面
     * @param request
     * @return
     */
    @RequestMapping("nto_reply_edit")
    public String toReplyEdit( HttpServletRequest request ){
        String id = request.getParameter("id");
        request.setAttribute( "list", propertyVoiceService.getComplaintManageInfo( id ) );
        return "propertyComplaintManage/replyEdit";
    }

    /**
     * 回复内容修改
     * @param voice
     * @param response
     */
    @RequestMapping("nreply_edit")
    public void replyEdit (ComplaintManage voice , HttpServletResponse response) {

        String code = "200";
        String meg = "修改成功";
        try{
            int success = propertyVoiceService.complaintManageEdit(voice);
            if( success == 0 ){
                code = "300";
                meg = "修改失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "修改失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "nto_voice_reply", "", "closeCurrent", ""));
    }

    /**
     * 删除
     * @param request
     */
    @RequestMapping("nreply_delete")
    public void replyDelete (HttpServletRequest request,HttpServletResponse response) {
        String code = "200";
        String meg = "删除成功";
        try{
            String id = request.getParameter("id");
            int success = propertyVoiceService.updateStatus(id,"未处理");
            if( success == 0 ){
                code = "300";
                meg = "删除失败";
            }
        }catch (Exception e) {
            code = "300";
            meg = "删除失败";
        }
        RespUtil.sendJson(response, DwzUtil.getRetAjaxJson(code, meg, "nto_voice_reply", "", "", ""));
    }
}
